import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from "axios";
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.axios = axios

//将此请求头加入到之后的每一次请求之中
axios.interceptors.request.use(
    config => {
        const tokenStr = window.localStorage.getItem('lmToken')
        console.log(tokenStr)
        if (tokenStr) {
            config.headers['Authorization'] = tokenStr
        }
        return config
    }
)

//在路由跳转之前执行
router.beforeEach((to, from, next) => {
    const tokenStr = window.localStorage.getItem('lmToken');
    if (to.path == "/hall" || to.path == '/login' || to.path == '/loveStory' || to.path == '/memory' ||
        to.path == '/letter' || to.path == '/food' || to.path == '/location') {
        next();
    } else if (!tokenStr) {
        next({
            path: '/login'
        });
    } else {
        next();
    }
});


axios.interceptors.response.use(response => {
        console.log('响应回来：' + response.data.state)
        if (response.data.state == 40900) {
            // 引用element ui message提示框
            ElementUI.Message({
                message: '身份已失效,请重新登录',
                type: 'error'
            });
            localStorage.removeItem('lmToken');
            //跳转
            console.log("即将跳转页面")
            router.push("/hall");

        } else if (response.data.state == 40901 || response.data.state == 40902 || response.data.state == 50002) {
            ElementUI.Message({
                message: response.data.message,
                type: 'error'
            });
            localStorage.removeItem('lmToken');
            //跳转
            router.push("/hall");
        } else {
            return response
        }
    },
)


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

